﻿package slim3.demo.controller.six;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestURLStatus {
    public static void main(String args[]) {
        try {
//            java.net.URL url = new java.net.URL("http://tw.yahoo.com/123123");
//            java.net.URL url = new java.net.URL("http://www.taoyuan-airport.com");
            
//            java.net.URL url = new java.net.URL("http://www.taoyuan-airport.com/english/schedule_e.jsp?airline=null&area=null&apio=A&airtype=P&apcode=null&dayindex=4&viewall=yes&cnid=508");

            String temp;
            
//            temp = "www.google.omc";
//            temp = "www.taoyuan-airport.com/english/schedule_e.jsp?airline=null&area=null&apio=A&airtype=P&apcode=null&dayindex=4&viewall=yes&cnid=508";
            temp = "www.tsa.gov.tw/woefjeow";
            java.net.URL url = new java.net.URL("http://" + temp);
            
            
            java.net.HttpURLConnection uc = (java.net.HttpURLConnection) url
                    .openConnection();
            uc.setRequestProperty("User-agent", "IE/6.0");
            uc.setReadTimeout(30000);// 設定timeout時間
            uc.connect();// 連線
            System.out.println("網址/ip位置:"+java.net.Inet4Address.getByName(url.getHost()));
            int status = uc.getResponseCode();
            System.out.println(status);
            switch (status) {
            case java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT://504
                System.out.println("連線網址逾時!");
                break;
            case java.net.HttpURLConnection.HTTP_FORBIDDEN://403
                System.out.println("連線網址禁止!");
                break;
            case java.net.HttpURLConnection.HTTP_INTERNAL_ERROR://500
                System.out.println("連線網址錯誤或不存在!");
                break;
            case java.net.HttpURLConnection.HTTP_NOT_FOUND://404
                System.out.println("連線網址不存在!");
                break;
            case java.net.HttpURLConnection.HTTP_OK:
                System.out.println("OK!");
                break;
 
            }
            
            
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));

            String inputLine;
            inputLine = in.readLine();
            if (inputLine == null) {
                System.out.println("null");
            } else {
                System.out.println("has something");
            }
//                while ((inputLine = in.readLine()) != null) {
//                    System.out.println(inputLine);
//                }
            in.close();
 
        } catch (java.net.MalformedURLException e) {
            System.out.println("網址格式錯誤!!!");
            e.printStackTrace();
        } catch (java.io.IOException e) {
            System.out.println("連線有問題!!!!!!");
            e.printStackTrace();
        }
    }
}